package com.dormitoryAdministratorSystem.dao.room.impl;

import com.dormitoryAdministratorSystem.dao.room.IRoomDao;
import com.dormitoryAdministratorSystem.entity.room.RoomBean;
import com.dormitoryAdministratorSystem.util.DBUtil;
import com.dormitoryAdministratorSystem.util.DBUtil2;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class RoomDaoImpl implements IRoomDao {
    @Override
    public List<RoomBean> getCanLiveRoom() {
        String sql = "select " +
                "rid, " +
                "address, " +
                "size, " +
                "peopleNo, " +
                "rent, " +
                "roomState, " +
                "hostName, " +
                "hostTel, " +
                "roomType, " +
                "houseType," +
                " payMode," +
                " rentTime" +
                " from t_room where peopleNo < size;";

        return getList(sql);
    }

    @Override
    public int updateRoomInfo(int rId, int peopleNo) {
        String sql = "update t_room set peopleNo = peopleNo + ? where rid = ?";
        return DBUtil2.executeUpdate(sql,peopleNo,rId);
    }

    @Override
    public List<RoomBean> getRoomByPage(String sql, Object... parms) {
        return getList(sql,parms);
    }

    @Override
    public int getRoomTolNum(String sql, Object... parms) {
        int result = 0;

        ResultSet rSet = DBUtil.excuteQuery(sql,parms);
        try {
            while(rSet.next()){
                result = rSet.getInt("rNum");
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally{
            DBUtil.close();
        }

        return result;
    }

    @Override
    public int addRoomInfo(RoomBean r) {
        String sql = "insert into t_room values(null, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
        return DBUtil.excuteUpdate(
                sql,
                r.getAddress(),
                r.getSize(),
                r.getPeopleNo(),
                r.getRent(),
                r.getRoomState(),
                r.getHostName(),
                r.getHostTel(),
                r.getRoomType(),
                r.getHouseType(),
                r.getPayMode(),
                r.getRentTime()
        );
    }

    @Override
    public int deleteRoom(int rId) {
        return DBUtil.excuteUpdate(
                "delete from t_room where rid = ?",
                rId
        );
    }

    @Override
    public RoomBean getRoomInfo(int rId) {
        RoomBean r = null;
        String sql = "select " +
                "rid, " +
                "address, " +
                "size, " +
                "peopleNo, " +
                "rent, " +
                "roomState, " +
                "hostName, " +
                "hostTel, " +
                "roomType, " +
                "houseType," +
                " payMode," +
                " rentTime" +
                " from t_room where rid = ? ";

        try {
            ResultSet rSet = DBUtil.excuteQuery(sql,rId);
            while(rSet.next()){
                r = getRoomBean(rSet);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            DBUtil.close();
        }
        return r;
    }

    @Override
    public int changeRoomState(String state, int id) {
        String sql = "update t_room set roomState = ? where rid = ?";

        return DBUtil.excuteUpdate(sql, state, id);
    }

    @Override
    public List<RoomBean> getAllRooms() {
        return getList("select * from t_room");
    }

    private RoomBean getRoomBean(ResultSet rSet) throws SQLException {
        RoomBean r = new RoomBean();
        r.setRId(rSet.getInt("rid"));
        r.setAddress(rSet.getString("address"));
        r.setSize(rSet.getInt("size"));
        r.setPeopleNo(rSet.getInt("peopleNo"));
        r.setRent(rSet.getDouble("rent"));
        r.setRoomState(rSet.getString("roomState"));
        r.setHostName(rSet.getString("hostName"));
        r.setHostTel(rSet.getString("hostTel"));
        r.setRoomType(rSet.getString("roomType"));
        r.setHouseType(rSet.getString("houseType"));
        r.setPayMode(rSet.getString("payMode"));
        r.setRentTime(rSet.getDate("rentTime"));
        r.setStudents(null);
        r.setMaintains(null);
        return r;
    }

    private List<RoomBean> getList(String sql, Object... parms){
        List<RoomBean> list = new ArrayList<>();
        ResultSet rSet = DBUtil.excuteQuery(sql,parms);
        try {
            while(rSet.next()){
                list.add(getRoomBean(rSet));
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            DBUtil.close();
        }

        return list;
    }
}
